package com.system.goods.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.common.bean.Address;
import com.system.common.bean.Admin;
import com.system.common.bean.Type;
import com.system.common.exception.ServiceException;
import com.system.common.service.ITypeService;
import com.system.goods.mapper.TypeMapper;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements ITypeService {
    @Override
    public Type getTypeById(Integer id) {
        return super.getById(id);
    }

    @Override
    public Type getTypeByName(String name) {
        return super.getOne(new QueryWrapper<Type>().eq("name", name));
    }

    @Override
    public void addType(Type type) {
        super.save(type);
    }

    @Override
    public void updateType(Type type) {
        super.updateById(type);

    }

    @Override
    public void deleteType(Integer id) {
        Type type = super.getById(id);
        if (type == null) {
            throw new ServiceException("类型不存在");
        }
        if (type.getIsdel() == 1) {
            throw new ServiceException("类型已删除");
        }
        super.removeById(id);
    }

    @Override
    public List<Type> getTypeList() {
        return super.list();
    }

    @Override
    public Page<Type> getPage(Type type, Page<Type> page) {
       LambdaQueryWrapper<Type> queryWrapper = new LambdaQueryWrapper<>();
       Page<Type> resultPage = baseMapper.selectPage(page, queryWrapper);
       Long total = this.count(queryWrapper);
       resultPage.setTotal(total);
       return resultPage;
    }


}
